<!doctype html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>后台登录-X-admin2.2</title>
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <link rel="stylesheet" href="/X-admin/css/font.css">
    <link rel="stylesheet" href="/X-admin/css/login.css">
    <link rel="stylesheet" href="/X-admin/css/xadmin.css">
    <script src="/X-admin/lib/layui/layui.js" charset="utf-8"></script>
    <script src="js/jquery.min.js"></script>
    <link rel="stylesheet" href="css/x-login.css">
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body class="login-bg">

<div class="login layui-anim layui-anim-up">
    <div class="message">x-admin2.0-管理登录</div>
    <div id="darkbannerwrap"></div>
    <form method="post" class="layui-form">
        <input name="username" id="username" placeholder="用户名" type="text" lay-verify="username" class="layui-input">
        <hr class="hr15">
        <input name="password" id="password" lay-verify="pass" placeholder="密码" type="password" class="layui-input">
        <hr class="hr15">
        <div class="code">
            <input name="code" id="code" lay-verify="code" placeholder="验证码" type="text" class="layui-input">
            <img src="/code" id="code_img">
        </div>
        <hr class="hr15">
        <input value="登录" lay-submit lay-filter="login" style="width:100%;" type="submit">
        <hr class="hr20">
    </form>
</div>

<script>

    $(function () {
        layui.use('form', function () {
            var form = layui.form;
            // layer.msg('玩命卖萌中', function(){
            //   //关闭后的操作
            //   });

            //监听提交
            form.on('submit(login)', function (data) {
                // alert(888)
                // layer.msg(JSON.stringify(data.field), function () {
                //     // location.href = 'index.html'
                // });
                // console.log(typeof data.field);
                let field = {
                    username: $("#username").val(),
                    password: $("#password").val(),
                    code: $("#code").val()
                };
                $.ajax({
                    url: '/login',//地址：访问后台的地址
                    data: field,//前端传递给后台的数据
                    type: 'post',
                    dataType: 'json',//是后端传递到前端的数据格式
                    success: function (data) {//后端给前端的一个反馈
                        console.log(data);
                        if (data.result == "error-code") {
                            layer.msg("验证码不正确");
                        }
                        if (data.result == "error") {
                            layer.msg("用户名和账号不存在");
                        }
                        if (data.result == "success") {
                            layer.msg("即将进入管理页面", function () {
                                //登录成功以后，需要将用户名传递到另一个页面
                                //js提供了两个容器，session  local
                                sessionStorage.setItem("loginName", $("#username").val());
                                localStorage.setItem("loginName", $("#username").val());
                                window.open("/html/index.html", "_self");
                            });
                        }
                    }

                });
                return false;
            });

            //自定义验证规则
            form.verify({
                username: [/^[\w-]{4,16}$/, "你输入的格式不正确"],
                pass: [/(.+){6,12}$/, '密码必须6到12位']

            });
        });
        $('#code_img').click(function (e) {
            $("#code_img").attr('src', "/code?random=" + Math.random());
        });
    })
</script>
<!-- 底部结束 -->
<script>
    //百度统计可去掉
    var _hmt = _hmt || [];
    (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();
</script>
</body>
</html>